package com.meihuayishu.backend.algorithm;

import com.meihuayishu.backend.algorithm.CommonCalculator.MeihuaResult;
import java.util.HashMap;
import java.util.Map;

public class WuxingAnalyzer {
    
    // 五行相生关系
    private static final Map<String, String> SHENG_RELATION = new HashMap<>();
    
    // 五行相克关系
    private static final Map<String, String> KE_RELATION = new HashMap<>();
    
    // 八卦对应的五行
    private static final Map<String, String> GUA_WUXING = new HashMap<>();
    
    static {
        // 初始化五行相生关系
        SHENG_RELATION.put("金", "水");
        SHENG_RELATION.put("水", "木");
        SHENG_RELATION.put("木", "火");
        SHENG_RELATION.put("火", "土");
        SHENG_RELATION.put("土", "金");
        
        // 初始化五行相克关系
        KE_RELATION.put("金", "木");
        KE_RELATION.put("木", "土");
        KE_RELATION.put("土", "水");
        KE_RELATION.put("水", "火");
        KE_RELATION.put("火", "金");
        
        // 初始化八卦对应的五行
        GUA_WUXING.put("乾", "金");
        GUA_WUXING.put("兑", "金");
        GUA_WUXING.put("坤", "土");
        GUA_WUXING.put("艮", "土");
        GUA_WUXING.put("震", "木");
        GUA_WUXING.put("巽", "木");
        GUA_WUXING.put("坎", "水");
        GUA_WUXING.put("离", "火");
    }
    
    /**
     * 获取卦象对应的五行
     * @param gua 卦象名称
     * @return 五行属性
     */
    public static String getWuxing(String gua) {
        return GUA_WUXING.getOrDefault(gua, "未知");
    }
    
    /**
     * 分析两个五行之间的关系
     * @param wuxing1 五行1
     * @param wuxing2 五行2
     * @return 关系描述
     */
    public static String analyzeRelation(String wuxing1, String wuxing2) {
        // 处理空值情况
        if (wuxing1 == null || wuxing2 == null) {
            return "无关系";
        }
        
        if (wuxing1.equals(wuxing2)) {
            return "比和";
        }
        
        // 获取相生相克关系
        String shengRelation = SHENG_RELATION.get(wuxing1);
        String keRelation = KE_RELATION.get(wuxing1);
        String shengRelation2 = SHENG_RELATION.get(wuxing2);
        String keRelation2 = KE_RELATION.get(wuxing2);
        
        // 检查相生相克关系
        if (shengRelation != null && shengRelation.equals(wuxing2)) {
            return "上生下";
        } else if (keRelation != null && keRelation.equals(wuxing2)) {
            return "上克下";
        } else if (shengRelation2 != null && shengRelation2.equals(wuxing1)) {
            return "下生上";
        } else if (keRelation2 != null && keRelation2.equals(wuxing1)) {
            return "下克上";
        } else {
            return "无关系";
        }
    }
    
    /**
     * 根据生克关系判断吉凶
     * @param relation 生克关系
     * @param tiShang 体卦位置（上或下）
     * @return 吉凶判断
     */
    public static String judgeFortune(String relation, String tiShang) {
        switch (relation) {
            case "用生体":
            case "比和":
                return "吉";
            case "体生用":
            case "克体":
                return "凶";
            default:
                return "中平";
        }
    }
    
    /**
     * 分析卦象之间的五行关系
     * @param result 梅花易数计算结果
     * @return 五行关系分析结果
     */
    public static WuxingRelationResult analyzeGuaRelation(MeihuaResult result) {
        // 获取各卦的五行属性
        String upWuxing = getWuxing(result.getGuaUp());
        String downWuxing = getWuxing(result.getGuaDown());
        String bodyWuxing = getWuxing(result.getGuaBody());
        String useWuxing = getWuxing(result.getGuaUse());
        String huUpWuxing = getWuxing(result.getGuaHuUp());
        String huDownWuxing = getWuxing(result.getGuaHuDown());
        String changeWuxing = getWuxing(result.getGuaChange());
        
        // 分析主卦关系
        String mainRelation = analyzeRelation(upWuxing, downWuxing);
        
        // 分析体用关系
        String bodyUseRelation = analyzeRelation(bodyWuxing, useWuxing);
        
        // 分析互卦关系
        String huRelation = analyzeRelation(huUpWuxing, huDownWuxing);
        
        // 分析变卦关系
        String changeRelation = analyzeRelation(bodyWuxing, changeWuxing);
        
        return new WuxingRelationResult(
            upWuxing, 
            downWuxing, 
            bodyWuxing, 
            useWuxing, 
            huUpWuxing, 
            huDownWuxing, 
            changeWuxing,
            mainRelation, 
            bodyUseRelation, 
            huRelation, 
            changeRelation
        );
    }
    
    /**
     * 五行关系分析结果类
     */
    public static class WuxingRelationResult {
        private String upWuxing;        // 上卦五行
        private String downWuxing;      // 下卦五行
        private String bodyWuxing;      // 体卦五行
        private String useWuxing;       // 用卦五行
        private String huUpWuxing;      // 互卦上卦五行
        private String huDownWuxing;    // 互卦下卦五行
        private String changeWuxing;    // 变卦五行
        private String mainRelation;    // 主卦关系
        private String bodyUseRelation; // 体用关系
        private String huRelation;      // 互卦关系
        private String changeRelation;  // 变卦关系

        public WuxingRelationResult() {
        }

        public WuxingRelationResult(String upWuxing, String downWuxing, String bodyWuxing, String useWuxing,
                                   String huUpWuxing, String huDownWuxing, String changeWuxing,
                                   String mainRelation, String bodyUseRelation, String huRelation, String changeRelation) {
            this.upWuxing = upWuxing;
            this.downWuxing = downWuxing;
            this.bodyWuxing = bodyWuxing;
            this.useWuxing = useWuxing;
            this.huUpWuxing = huUpWuxing;
            this.huDownWuxing = huDownWuxing;
            this.changeWuxing = changeWuxing;
            this.mainRelation = mainRelation;
            this.bodyUseRelation = bodyUseRelation;
            this.huRelation = huRelation;
            this.changeRelation = changeRelation;
        }

        // Getters and Setters
        public String getUpWuxing() {
            return upWuxing;
        }

        public void setUpWuxing(String upWuxing) {
            this.upWuxing = upWuxing;
        }

        public String getDownWuxing() {
            return downWuxing;
        }

        public void setDownWuxing(String downWuxing) {
            this.downWuxing = downWuxing;
        }

        public String getBodyWuxing() {
            return bodyWuxing;
        }

        public void setBodyWuxing(String bodyWuxing) {
            this.bodyWuxing = bodyWuxing;
        }

        public String getUseWuxing() {
            return useWuxing;
        }

        public void setUseWuxing(String useWuxing) {
            this.useWuxing = useWuxing;
        }

        public String getHuUpWuxing() {
            return huUpWuxing;
        }

        public void setHuUpWuxing(String huUpWuxing) {
            this.huUpWuxing = huUpWuxing;
        }

        public String getHuDownWuxing() {
            return huDownWuxing;
        }

        public void setHuDownWuxing(String huDownWuxing) {
            this.huDownWuxing = huDownWuxing;
        }

        public String getChangeWuxing() {
            return changeWuxing;
        }

        public void setChangeWuxing(String changeWuxing) {
            this.changeWuxing = changeWuxing;
        }

        public String getMainRelation() {
            return mainRelation;
        }

        public void setMainRelation(String mainRelation) {
            this.mainRelation = mainRelation;
        }

        public String getBodyUseRelation() {
            return bodyUseRelation;
        }

        public void setBodyUseRelation(String bodyUseRelation) {
            this.bodyUseRelation = bodyUseRelation;
        }

        public String getHuRelation() {
            return huRelation;
        }

        public void setHuRelation(String huRelation) {
            this.huRelation = huRelation;
        }

        public String getChangeRelation() {
            return changeRelation;
        }

        public void setChangeRelation(String changeRelation) {
            this.changeRelation = changeRelation;
        }

        @Override
        public String toString() {
            return "WuxingRelationResult{" +
                    "upWuxing='" + upWuxing + '\'' +
                    ", downWuxing='" + downWuxing + '\'' +
                    ", bodyWuxing='" + bodyWuxing + '\'' +
                    ", useWuxing='" + useWuxing + '\'' +
                    ", huUpWuxing='" + huUpWuxing + '\'' +
                    ", huDownWuxing='" + huDownWuxing + '\'' +
                    ", changeWuxing='" + changeWuxing + '\'' +
                    ", mainRelation='" + mainRelation + '\'' +
                    ", bodyUseRelation='" + bodyUseRelation + '\'' +
                    ", huRelation='" + huRelation + '\'' +
                    ", changeRelation='" + changeRelation + '\'' +
                    '}';
        }
    }
}